package com.example.workbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.workbook.common.R;
import com.example.workbook.model.domain.User;
import com.example.workbook.model.request.UserRequest;
import com.example.workbook.service.UserService;
import com.example.workbook.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

import static com.example.workbook.constant.Constants.SALT;

/**
* @author 53067
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-08-09 14:26:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;

    /**
     * 判断用户是否已存在
     * @param userName
     * @return
     */
    public boolean existUser(String userName) {
        LambdaQueryWrapper<User> consumerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        consumerLambdaQueryWrapper.eq(User::getUsername,userName);
        return userMapper.selectCount(consumerLambdaQueryWrapper) > 0;
    }

    /**
     * 用户注册
     * @param registryRequest
     * @return
     */
    @Override
    public R registryUser(UserRequest registryRequest) {
        if (this.existUser(registryRequest.getUsername())) {
            return R.warning("用户名已注册");
        }
        User user = new User();
        BeanUtils.copyProperties(registryRequest,user);
        //MD5加密
        String password = DigestUtils.md5DigestAsHex((SALT + registryRequest.getPassword()).getBytes(StandardCharsets.UTF_8));
        user.setPassword(password);
        try {
            if (userMapper.insert(user) > 0) {
                return R.success("注册成功");
            }else {
                return R.error("注册失败");
            }
        }catch (DuplicateKeyException duplicateKeyException) {
            return R.fatal(duplicateKeyException.getMessage());
        }
    }
    /**
     * 验证密码
     * @param userName
     * @param password
     * @return
     */
    public boolean verityPassword(String userName,String password) {
        LambdaQueryWrapper<User> consumerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        consumerLambdaQueryWrapper.eq(User::getUsername,userName);
        //密码解密
        String secretPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes(StandardCharsets.UTF_8));

        consumerLambdaQueryWrapper.eq(User::getPassword,secretPassword);
        return userMapper.selectCount(consumerLambdaQueryWrapper) > 0;
    }
    @Override
    public R userLogin(UserRequest loginRequest, HttpSession httpSession) {

        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        if (this.verityPassword(username,password)) {
            httpSession.setAttribute("username",username);
            User user = new User();
            user.setUsername(username);
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getUsername,username);
            return R.success("登录成功",userMapper.selectList(userLambdaQueryWrapper));
        }else {
            return R.error("用户名或密码错误");
        }
    }

    @Override
    public R allUser() {

        return R.success(null,userMapper.getUsersWithContent());
    }

    @Override
    public R userOfId(int id) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId,id);
        return R.success(null,userMapper.selectList(userLambdaQueryWrapper));
    }
}




